package com.itheima02;

import javax.imageio.stream.FileImageOutputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class Test02 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        HashMap<Character,Integer> hm =new HashMap<>();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("day11_demo//info2.txt"));
        System.out.println("请输入一个字符串:");
        String str = sc.nextLine();
        ArrayList<Character> arrayList = new ArrayList<>();
        int by ;
        while ((by = bis.read())!=-1){
            arrayList.add((char)by);
        }
        //String s = arrayList.toString();
        System.out.println(arrayList);
        Character[] chs = arrayList.toArray(new Character[arrayList.size()]);
        String s="";
        for (Character ch : chs) {
            s = s+ch.toString();
        }
        //String s = chs.toString();
        //char[] chs1= chs;
        //String s = String.valueOf(chs);
        System.out.println(s);
        int i;
        int count=0;
        while ((i=s.indexOf(str))!=-1){
            s = s.substring(i+str.length());
            count++;
        }
        System.out.println(str+"出现次数"+count+"次");

        for (Character character : arrayList) {
            if(hm.containsKey(character)){
                Integer integer = hm.get(character);
                integer++;
                hm.put(character,integer);
            }else {
                hm.put(character,1);
            }
        }
        Set<Character> characters = hm.keySet();
        for (Character character : characters) {
            Integer integer = hm.get(character);
            System.out.println(character+"出现了"+integer+"次");
        }


        bis.close();
    }
}
